package com.sw.bbs.board.controller;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.sw.bbs.board.service.MediaService;
import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.common.common.ServiceException;
import com.sw.bbs.common.util.ConstantQiniu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Desc: No Desc
 * @Author: wusonghe
 * @Date: 2019/3/12 16:48
 */
@Api(value = "文件模块", description = "文件", tags = {"File Module"})
@RestController("/media/")
@Slf4j
public class MediaController {



    @Autowired
    private MediaService mediaService;

    /**
     * 上传文件到七牛云存储
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/uploadFile")
    public ResponseEntity<?> upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        try {

            String result = mediaService.upload(multipartFile);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/file/upload", e);
            throw new GlobalException("/file/upload",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }

    }

    @PostMapping("/addAlbum")
    @ApiOperation(value = "新建图册接口",notes = "新建图册", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "新建图册", response = ResponseEntity.class)})
    public ResponseEntity<?> becomeAlbum(@RequestParam("medias") List<String> urls, HttpServletRequest httpServletRequest) throws IOException {

        try {
            String result = null;//mediaService.upload(multipartFile);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (ServiceException e) {
            log.error("/file/becomeAlbum", e);
            throw new GlobalException("/file/becomeAlbum",e.getErrorCode().getMsg(),e.getErrorCode().getCode());
        }

    }




}
